//
//  CYWenDaDiCengTuiJianTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/2/13.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYWenDaDiCengTuiJianTableViewCell.h"

@implementation CYWenDaDiCengTuiJianTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headImageView.layer.cornerRadius = 25 / 2;
    self.headImageView.layer.masksToBounds = YES;
    
    self.guanzhuBtn.layer.cornerRadius = 3;
    self.guanzhuBtn.layer.masksToBounds = YES;
    self.guanzhuBtn.layer.borderWidth = 1;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeadImageView:)];
    [self.headImageView addGestureRecognizer:tap];
    self.headImageView.userInteractionEnabled = YES;
}

- (IBAction)guanzhuAction:(UIButton *)sender {
    if ([self.model.is_follow integerValue] == 1) {
        [self cancleguanzhu];
    }else{
        [self guanzhu];
    }
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    if ([model.is_follow integerValue] == 1) {
        [self.guanzhuBtn setTitle:@"已关注" forState:UIControlStateNormal];
        self.guanzhuBtn.layer.borderColor = RGBOF(0xe3e3e3).CGColor;
        [self.guanzhuBtn setBackgroundColor:[UIColor whiteColor]];
        [self.guanzhuBtn setTitleColor:RGBOF(0x999999) forState:UIControlStateNormal];
    }else{
        [self.guanzhuBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
        self.guanzhuBtn.layer.borderColor = RGBOF(0xFF5353).CGColor;
        [self.guanzhuBtn setBackgroundColor:RGBOF(0xFF5353)];
        [self.guanzhuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
    self.nickNameLabel.text = model.nickname;
    self.timeLabel.text = model.created_time;
    self.contentLabel.text = [[Tool sharedInstance] delHTMLBiqoQianWithHtml:model.content];

}

- (void)tapHeadImageView:(UITapGestureRecognizer *)tap{
    CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
    zhuye.userId = self.model.user_id;
    [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
}


#pragma mark - 关注
- (void)guanzhu{
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention"];
    NSDictionary *param = @{@"toUserId":self.model.user_id};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"添加关注 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_follow = [NSNumber numberWithInteger:1];
                [self setModel:self.model];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (void)cancleguanzhu{
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",User_HOST, @"attention", self.model.user_id];
    NSDictionary *param = @{@"userId":self.model.user_id};
    [[XBRequest sharedInstance] deleteDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"取消关注 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_follow = [NSNumber numberWithInteger:2];
                [self setModel:self.model];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

@end
